import {
  Controller,
  Get,
  Post,
  Body,
  HttpCode,
  Query,
  UseGuards,
  Headers,
} from '@nestjs/common';
import {
  ApiResponse,
  ApiOperation,
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
} from '@nestjs/swagger';
import { throwHttp } from 'src/utlis';
import {
  CreateRoleDto,
  GetRolePageDto,
  RoleAuthConfigDto,
  UpdateRoleDto,
} from './dto/role.dto';
import { RoleEntity } from './entities/role.entity';
import { RoleService } from './role.service';
import { Roles } from 'src/common/auth/role.decorator';
@ApiBearerAuth() // swagger文档设置token
@ApiTags('权限')
@Controller('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) { }
  /** 角色列表 */
  @HttpCode(200)
  @Get('/getRolePage')
  @ApiResponse({
    status: 200,
    description: '角色列表',
    type: [RoleEntity],
  })
  @ApiOperation({
    tags: ['获取角色列表'],
    summary: '获取角色列表',
    operationId: 'getRolePage',
  })
  getRolePage(@Query() params: GetRolePageDto) {
    return this.roleService.getRolePage(params);
  }

  @HttpCode(200)
  @Post('/createRole')
  @ApiParam({
    name: '创建角色',
    description: '创建角色',
    type: CreateRoleDto,
  })
  @ApiBody({
    description: '创建角色',
    type: CreateRoleDto,
  })
  @Roles('admin') // 自定义装璜器
  @ApiOperation({ summary: '创建角色', operationId: 'createRole' })
  public async createRole(@Body() body: CreateRoleDto, @Headers() headers) {
    return this.roleService.createValidateHeaders(body, headers);
  }
  @HttpCode(200)
  @Post('/updateRole')
  @ApiParam({
    name: '更新角色',
    description: '更新角色',
    type: UpdateRoleDto,
  })
  @ApiBody({
    description: '更新角色',
    type: UpdateRoleDto,
  })
  @Roles('admin') // 自定义装璜器
  @ApiOperation({ summary: '更新角色', operationId: 'updateRole' })
  public async updateRole(@Body() body: UpdateRoleDto) {
    const { success, message } = await this.roleService.updateRole(body);
    if (success) {
      return message;
    }
    throwHttp(message);
  }

  @HttpCode(200)
  @Post('/roleAuthConfig')
  @ApiParam({
    name: '角色权限配置',
    description: '角色权限配置',
    type: RoleAuthConfigDto,
  })
  @ApiBody({
    description: '角色权限配置',
    type: RoleAuthConfigDto,
  })
  @Roles('admin') // 自定义装璜器
  @ApiOperation({ summary: '角色权限配置', operationId: 'roleAuthConfig' })
  public async roleAuthConfig(@Body() body: RoleAuthConfigDto) {
    return this.roleService.roleAuthConfig(body);
  }

}
